package com.budwk.app.miniapp.services.impl;

import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.miniapp.models.WxEventPush;
import com.budwk.app.miniapp.services.WxeventpushService;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean(args = {"refer:dao"})
public class WxeventpushServiceImpl extends BaseServiceImpl<WxEventPush> implements WxeventpushService {
    public WxeventpushServiceImpl(Dao dao) {
        super(dao);
    }

/*
    //新建事件推送消息
    @Override
    public Boolean eventPushTakeNotes(WxEvent bo) {
        try {
            WxMaMsgService wxMaMsgService = wxMaService.getMsgService();

            ArrayList<String> openlist= getAlloPenId(bo.getLatitude(),bo.getLongitude());
            for (int i = 0; i < openlist.size(); i++) {

                // 3.8.0 版本使用的是Data
                List<WxMaSubscribeMessage.MsgData> data = new ArrayList<>();
                //下面的number1,thing3等数据，是需要根据申请的微信模板对应的，并不是每个人都一样
                WxMaSubscribeMessage.MsgData data1 = new WxMaSubscribeMessage.MsgData("thing1", "你好啊");
                WxMaSubscribeMessage.MsgData data2 = new WxMaSubscribeMessage.MsgData("car_number3", "京A00000");
                WxMaSubscribeMessage.MsgData data3 = new WxMaSubscribeMessage.MsgData("thing4", "超时提示");
                data.add(data1);
                data.add(data2);
                data.add(data3);
                // 3.8.0版本使用的使用WxMaSubscribeMessage
                WxMaSubscribeMessage subscribeMessage =
                        WxMaSubscribeMessage.builder()
                                //这里添加的是推送消息的目标对象openId
                                .toUser(openlist.get(i))
                                //这里填写的就是在后台申请添加的模板ID
                                .templateId(environment.getProperty("wx.miniapp.templateId"))
                                //添加请求参数
                                .data(data)
                                //添加跳转链接，如果目标用户点击了推送的消息，则会跳转到小程序主页
                                .page("/pages/index/index")
                                .build();

                //推送消息
                wxMaMsgService.sendSubscribeMsg(subscribeMessage);
                WxEventPushBo wbo = new WxEventPushBo();
                wbo.setEventTitle(bo.getEventTitle());
                // wbo.setEventId(bo.getId());
                wbo.setCreatime(new Date());
                wbo.setPushContent(data1.getValue()+data2.getValue()+data3.getValue());
                this.insertByBo(wbo);
            }
            return  true;
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return  false;
        }
    }

    public ArrayList<String> getAlloPenId(Double latitude,Double longitude) {
        ArrayList<String> result = new ArrayList<>();

        List<WxUser> ulist = RedisUtils.getCacheObject(CacheConstants.WX_ALL_USER_KEY);
        if(latitude==null||longitude==null||ulist==null) return null;

        for (WxUser vo:ulist) {
            if(vo.getLatitude()!=null&&vo.getLongitude()!=null) {
                //计算事件离我的距离
                Double distance = DistanceUtils.latitudeLongitudeDistance(latitude,longitude, vo.getLatitude(), vo.getLongitude());
                if(distance<5000) //小于5公里，发送事件创建信息
                {
                    result.add(vo.getOpenId());
                }
            }
        }

        return  result;
    }*/
}
